package com.yidu.hbats.service.TrainingPlan;

import com.yidu.hbats.dao.*;
import com.yidu.hbats.model.*;
import com.yidu.utils.AtsPlanItemTree;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hbats
 * @description 训练计划指标表
 * @author: 周佳佳
 * @create: 2019-11-05 08:52
 **/
@Service
public class AtsPlanItemServiceImpl implements AtsPlanItemService {
    @Resource
    AtsPlanItemMapper atsPlanItemMapper;

    @Resource
    AtsPlanCurriculumMapper atsPlanCurriculumMapper;

    @Resource
    AtsPlanWeekMapper atsPlanWeekMapper;

    @Resource
    AtsPlanPhaseMapper atsPlanPhaseMapper;

    @Resource
    AtsPlanYearMapper atsPlanYearMapper;

    @Resource
    AtsPlanYearMoreMapper atsPlanYearMoreMapper;

    @Override
    public Map<String, Object> curSelectPid(AtsPlanItem atsPlanItem,AtsPlanCurriculum atsPlanCurriculum) {
        atsPlanItem.setRemark("1");
        atsPlanCurriculum = atsPlanCurriculumMapper.findById(atsPlanCurriculum.getCurriculumId());
        List<AtsPlanItem> list = atsPlanItemMapper.selectPid(atsPlanItem);
        Map<String, Object> map = new HashMap<>();

        map.put("data", atsPlanCurriculum);
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> weekSelectPid(AtsPlanItem atsPlanItem, AtsPlanWeek atsPlanWeek) {
        atsPlanItem.setRemark("2");
        atsPlanWeek = atsPlanWeekMapper.findById(atsPlanWeek.getWeekId());
        List<AtsPlanItem> list = atsPlanItemMapper.selectPid(atsPlanItem);
        Map<String, Object> map = new HashMap<>();

        map.put("data", atsPlanWeek);
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> phaseSelectPid(AtsPlanItem atsPlanItem, AtsPlanPhase atsPlanPhase) {
        atsPlanItem.setRemark("3");
        atsPlanPhase = atsPlanPhaseMapper.findById(atsPlanPhase.getPhaseId());
        List<AtsPlanItem> list = atsPlanItemMapper.selectPid(atsPlanItem);
        Map<String, Object> map = new HashMap<>();

        map.put("data", atsPlanPhase);
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> yearSelectPid(AtsPlanItem atsPlanItem, AtsPlanYear atsPlanYear) {
        atsPlanItem.setRemark("4");
        atsPlanYear = atsPlanYearMapper.findById(atsPlanYear.getPid());
        List<AtsPlanItem> list = atsPlanItemMapper.selectPid(atsPlanItem);
        Map<String, Object> map = new HashMap<>();

        map.put("data", atsPlanYear);
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> yaerMoreSelectPid(AtsPlanItem atsPlanItem, AtsPlanYearMore atsPlanYearMore) {
        atsPlanItem.setRemark("5");
        atsPlanYearMore = atsPlanYearMoreMapper.findById(atsPlanYearMore.getPid());
        List<AtsPlanItem> list = atsPlanItemMapper.selectPid(atsPlanItem);
        Map<String, Object> map = new HashMap<>();

        map.put("data", atsPlanYearMore);
        map.put("list", list);
        return map;
    }


    @Override
    public List<AtsPlanItemTree> curEchar(AtsPlanItem atsPlanItem) {

        //模型集合
        List<AtsPlanItemTree> treeList = new ArrayList<>();

        //按计划名称分组
        List<AtsPlanItem> curNameAll = atsPlanItemMapper.curNameGroup(atsPlanItem);

        //将计划循环存入模型集合
        for (AtsPlanItem planItem : curNameAll) {
            AtsPlanItemTree atsPlanItemTree = new AtsPlanItemTree();
            atsPlanItemTree.setTitle(planItem.getCurriculumName());
            treeList.add(atsPlanItemTree);
        }


        //将所有指标全部存入每个计划
        for (AtsPlanItemTree planItemTree : treeList) {
            //指标集合
            List<AtsPlanItem> itemNameAll = atsPlanItemMapper.itemNameGroup(atsPlanItem);
            planItemTree.setItemNameAll(itemNameAll);
        }

        for (AtsPlanItemTree planItemTree : treeList) {

            for (AtsPlanItem planItem : planItemTree.getItemNameAll()) {
                atsPlanItem.setCurriculumName(planItemTree.getTitle());
                atsPlanItem.setItemName(planItem.getItemName());
                List<AtsPlanItem> list = atsPlanItemMapper.selectEchar(atsPlanItem);
                if (list.size() > 0) {
                    planItem.setItemValue(list.get(0).getItemValue());
                } else {
                    planItem.setItemValue("0");
                }
            }
        }


        //饼图
        List<AtsPlanItem> curPieChart = atsPlanItemMapper.curPieChart(atsPlanItem);

        for (AtsPlanItemTree planItemTree : treeList) {
            planItemTree.setPieChart(curPieChart);
        }

        return treeList;
    }

    @Override
    public List<AtsPlanItemTree> weekEchar(AtsPlanItem atsPlanItem) {
        //模型集合
        List<AtsPlanItemTree> treeList = new ArrayList<>();

        //按计划名称分组
        List<AtsPlanItem> weekNameGroup = atsPlanItemMapper.weekNameGroup(atsPlanItem);

        //将计划循环存入模型集合
        for (AtsPlanItem planItem : weekNameGroup) {
            AtsPlanItemTree atsPlanItemTree = new AtsPlanItemTree();
            atsPlanItemTree.setTitle(planItem.getWeekName());
            treeList.add(atsPlanItemTree);
        }

        //将所有指标全部存入每个计划
        for (AtsPlanItemTree planItemTree : treeList) {
            //指标集合
            List<AtsPlanItem> weekItemNameGroup = atsPlanItemMapper.weekItemNameGroup(atsPlanItem);
            planItemTree.setItemNameAll(weekItemNameGroup);
        }


        for (AtsPlanItemTree planItemTree : treeList) {

            for (AtsPlanItem planItem : planItemTree.getItemNameAll()) {
                atsPlanItem.setWeekName(planItemTree.getTitle());
                atsPlanItem.setItemName(planItem.getItemName());
                List<AtsPlanItem> list = atsPlanItemMapper.weekSelectEchar(atsPlanItem);
                if (list.size() > 0) {
                    planItem.setItemValue(list.get(0).getItemValue());
                } else {
                    planItem.setItemValue("0");
                }
            }
        }

        //饼图
        List<AtsPlanItem> weekPieChart = atsPlanItemMapper.weekPieChart(atsPlanItem);

        for (AtsPlanItemTree planItemTree : treeList) {
            planItemTree.setPieChart(weekPieChart);
        }


        return treeList;
    }

    @Override
    public List<AtsPlanItemTree> phaseEchar(AtsPlanItem atsPlanItem) {
        //模型集合
        List<AtsPlanItemTree> treeList = new ArrayList<>();

        //按计划名称分组
        List<AtsPlanItem> phaseNameGroup = atsPlanItemMapper.phaseNameGroup(atsPlanItem);

        //将计划循环存入模型集合
        for (AtsPlanItem planItem : phaseNameGroup) {
            AtsPlanItemTree atsPlanItemTree = new AtsPlanItemTree();
            atsPlanItemTree.setTitle(planItem.getPhaseName());
            treeList.add(atsPlanItemTree);
        }

        //将所有指标全部存入每个计划
        for (AtsPlanItemTree planItemTree : treeList) {
            //指标集合
            List<AtsPlanItem> phaseItemNameGroup = atsPlanItemMapper.phaseItemNameGroup(atsPlanItem);
            planItemTree.setItemNameAll(phaseItemNameGroup);
        }


        for (AtsPlanItemTree planItemTree : treeList) {

            for (AtsPlanItem planItem : planItemTree.getItemNameAll()) {
                atsPlanItem.setPhaseName(planItemTree.getTitle());
                atsPlanItem.setItemName(planItem.getItemName());
                List<AtsPlanItem> list = atsPlanItemMapper.phaseSelectEchar(atsPlanItem);
                if (list.size() > 0) {
                    planItem.setItemValue(list.get(0).getItemValue());
                } else {
                    planItem.setItemValue("0");
                }
            }
        }

        //饼图
        List<AtsPlanItem> phasePieChart = atsPlanItemMapper.phasePieChart(atsPlanItem);

        for (AtsPlanItemTree planItemTree : treeList) {
            planItemTree.setPieChart(phasePieChart);
        }


        return treeList;
    }

    @Override
    public List<AtsPlanItemTree> yearEchar(AtsPlanItem atsPlanItem) {
        //模型集合
        List<AtsPlanItemTree> treeList = new ArrayList<>();

        //按计划名称分组
        List<AtsPlanItem> yearNameGroup = atsPlanItemMapper.yearNameGroup(atsPlanItem);

        //将计划循环存入模型集合
        for (AtsPlanItem planItem : yearNameGroup) {
            AtsPlanItemTree atsPlanItemTree = new AtsPlanItemTree();
            atsPlanItemTree.setTitle(planItem.getPname());
            treeList.add(atsPlanItemTree);
        }

        //将所有指标全部存入每个计划
        for (AtsPlanItemTree planItemTree : treeList) {
            //指标集合
            List<AtsPlanItem> yearItemNameGroup = atsPlanItemMapper.yearItemNameGroup(atsPlanItem);
            planItemTree.setItemNameAll(yearItemNameGroup);
        }


        for (AtsPlanItemTree planItemTree : treeList) {
            for (AtsPlanItem planItem : planItemTree.getItemNameAll()) {
                atsPlanItem.setPname(planItemTree.getTitle());
                atsPlanItem.setItemName(planItem.getItemName());
                List<AtsPlanItem> list = atsPlanItemMapper.yearSelectEchar(atsPlanItem);
                if (list.size() > 0) {
                    planItem.setItemValue(list.get(0).getItemValue());
                } else {
                    planItem.setItemValue("0");
                }
            }
        }

        //饼图
        List<AtsPlanItem> yearPieChart = atsPlanItemMapper.yearPieChart(atsPlanItem);

        for (AtsPlanItemTree planItemTree : treeList) {
            planItemTree.setPieChart(yearPieChart);
        }


        return treeList;
    }

    @Override
    public List<AtsPlanItemTree> yearMoreEchar(AtsPlanItem atsPlanItem) {
        //模型集合
        List<AtsPlanItemTree> treeList = new ArrayList<>();

        //按计划名称分组
        List<AtsPlanItem> yearMoreNameGroup = atsPlanItemMapper.yearMoreNameGroup(atsPlanItem);

        //将计划循环存入模型集合
        for (AtsPlanItem planItem : yearMoreNameGroup) {
            AtsPlanItemTree atsPlanItemTree = new AtsPlanItemTree();
            atsPlanItemTree.setTitle(planItem.getPname());
            treeList.add(atsPlanItemTree);
        }

        //将所有指标全部存入每个计划
        for (AtsPlanItemTree planItemTree : treeList) {
            //指标集合
            List<AtsPlanItem> yearMoreItemNameGroup = atsPlanItemMapper.yearMoreItemNameGroup(atsPlanItem);
            planItemTree.setItemNameAll(yearMoreItemNameGroup);
        }


        for (AtsPlanItemTree planItemTree : treeList) {

            for (AtsPlanItem planItem : planItemTree.getItemNameAll()) {
                atsPlanItem.setPname(planItemTree.getTitle());
                atsPlanItem.setItemName(planItem.getItemName());
                List<AtsPlanItem> list = atsPlanItemMapper.yearMoreSelectEchar(atsPlanItem);
                if (list.size() > 0) {
                    planItem.setItemValue(list.get(0).getItemValue());
                } else {
                    planItem.setItemValue("0");
                }
            }
        }

        //饼图
        List<AtsPlanItem> yearMorePieChart = atsPlanItemMapper.yearMorePieChart(atsPlanItem);

        for (AtsPlanItemTree planItemTree : treeList) {
            planItemTree.setPieChart(yearMorePieChart);
        }


        return treeList;
    }




    @Override
    public void insertSelective(AtsPlanItem record) {
        atsPlanItemMapper.insertSelective(record);
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMore(Integer pid) {
        List<AtsPlanItem> list = atsPlanItemMapper.findPlanItemYearMore(pid);
        return list;
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMoreone(Integer pid) {
        List<AtsPlanItem> list = atsPlanItemMapper.findPlanItemYearMoreone(pid);
        return list;
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMorethree(Integer pid) {
        return atsPlanItemMapper.findPlanItemYearMorethree(pid);
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMorefour(Integer pid) {
        return atsPlanItemMapper.findPlanItemYearMorefour(pid);
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMorefive(Integer pid) {
        return atsPlanItemMapper.findPlanItemYearMorefive(pid);
    }

    @Override
    public List<AtsPlanItem> findPlanItemYearMoretwo(Integer pid) {
        return atsPlanItemMapper.findPlanItemYearMoretwo(pid);
    }

    @Override
    public int updateByPrimaryKeySelective(AtsPlanItem record) {
        return atsPlanItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateOrdeletethree(AtsPlanItem record) {
        return atsPlanItemMapper.updateOrdeletethree(record);
    }

    @Override
    public int updateOrdelete(AtsPlanItem record) {
        return atsPlanItemMapper.updateOrdelete(record);
    }

    @Override
    public int updateOrdeletetwo(AtsPlanItem record) {
        return atsPlanItemMapper.updateOrdeletetwo(record);
    }

    @Override
    public List<AtsPlanItem> findDivone() {
        return atsPlanItemMapper.findDivone();
    }

    @Override
    public List<AtsPlanItem> findDivtwo() {
        return atsPlanItemMapper.findDivtwo();
    }

    @Override
    public List<AtsPlanItem> findDivthree() {
        return atsPlanItemMapper.findDivthree();
    }

    @Override
    public List<AtsPlanItem> findDivfour() {
        return atsPlanItemMapper.findDivfour();
    }

    @Override
    public List<AtsPlanItem> findDivfive() {
        return atsPlanItemMapper.findDivfive();
    }

    @Override
    public int updateByPrimaryKeySelectivetwo(AtsPlanItem record) {
        return atsPlanItemMapper.updateByPrimaryKeySelectivetwo(record);
    }

    @Override
    public int updateByPrimaryKeySelectivethree(AtsPlanItem record) {
        return atsPlanItemMapper.updateByPrimaryKeySelectivethree(record);
    }

    @Override
    public int updateByPrimaryKeySelectivefour(AtsPlanItem record) {
        return atsPlanItemMapper.updateByPrimaryKeySelectivefour(record);
    }

    @Override
    public int updateByPrimaryKeySelectivefive(AtsPlanItem record) {
        return atsPlanItemMapper.updateByPrimaryKeySelectivefive(record);
    }

    @Override
    public int updateOrdeletefour(AtsPlanItem record) {
        return atsPlanItemMapper.updateOrdeletefour(record);
    }

    @Override
    public int updateOrdeletefive(AtsPlanItem record) {
        return atsPlanItemMapper.updateOrdeletefive(record);
    }
}
